package co.yixiang.modules.supplier.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.supplier.domain.SupplierGoodsSku;
import co.yixiang.modules.supplier.domain.SupplierGoodsSkuLog;
import co.yixiang.modules.supplier.service.SupplierGoodsSkuLogService;
import co.yixiang.modules.supplier.service.SupplierGoodsSkuService;
import co.yixiang.modules.supplier.service.dto.SupplierGoodsSkuQueryCriteria;
import co.yixiang.utils.SecurityUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2021-03-22
*/
@Api(tags = "供货代理商品表管理")
@RestController
@RequestMapping("/api/supplierGoodsSku")
public class SupplierGoodsSkuController {

    private final SupplierGoodsSkuService supplierGoodsSkuService;
    private final SupplierGoodsSkuLogService supplierGoodsSkuLogService;

    public SupplierGoodsSkuController(SupplierGoodsSkuService supplierGoodsSkuService,SupplierGoodsSkuLogService supplierGoodsSkuLogService) {
        this.supplierGoodsSkuService = supplierGoodsSkuService;
        this.supplierGoodsSkuLogService = supplierGoodsSkuLogService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('supplierGoodsSku:list')")
    public void download(HttpServletResponse response, SupplierGoodsSkuQueryCriteria criteria) throws IOException {
        supplierGoodsSkuService.download(supplierGoodsSkuService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询供货代理商品表")
    @ApiOperation("查询供货代理商品表")
    @PreAuthorize("@el.check('supplierGoodsSku:list')")
    public ResponseEntity<Object> getSupplierGoodsSkus(SupplierGoodsSkuQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(supplierGoodsSkuService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增供货代理商品表")
    @ApiOperation("新增供货代理商品表")
    @PreAuthorize("@el.check('supplierGoodsSku:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SupplierGoodsSku resources){
        resources.setCreateUser(SecurityUtils.getUsername());
        resources.setStatus(0);

        return new ResponseEntity<>(supplierGoodsSkuService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改供货代理商品表")
    @ApiOperation("修改供货代理商品表")
    @PreAuthorize("@el.check('supplierGoodsSku:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SupplierGoodsSku resources){
        supplierGoodsSkuService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除供货代理商品表")
    @ApiOperation("删除供货代理商品表")
    @PreAuthorize("@el.check('supplierGoodsSku:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        supplierGoodsSkuService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("发布供货代理商品表")
    @ApiOperation("发布供货代理商品表")
    @PreAuthorize("@el.check('supplierGoodsSku:publish')")
    @PostMapping(value = "/publish")
    public ResponseEntity<Object> publish(@RequestBody SupplierGoodsSku resources){
        supplierGoodsSkuService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("生成30天价格数据")
    @ApiOperation("生成30天价格数据")
    @PreAuthorize("@el.check('supplierGoodsSku:makeprice')")
    @PostMapping(value = "/makePrice")
    public ResponseEntity<Object> makePrice(@RequestBody JSONObject params){
        Long skuId = params.getLong("skuId");
        Integer capacity = params.getInteger("capacity");
        supplierGoodsSkuLogService.make30dayPrice(skuId, capacity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
